/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-3         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.widget.ArcTranslateAnimation;
import com.tianci.appstore.utils.SkyConstants;

public class MoveSelectDialog extends BaseDialog implements OnFocusChangeListener {

    private TextView mTitle = null;
    private TextView mNative = null;
    private TextView mSdcard = null;
    private ImageView mDirectionView = null;
    private ArcTranslateAnimation mRightAnimation = null;
    private ArcTranslateAnimation mLeftAnimation = null;

    public MoveSelectDialog(Context context) {
        super(context);
        initUI();
    }

    protected void initUI() {
        addBackgroundView();
        addDirectionView();
        addTitleView();
        addItems();
    }

    private void addBackgroundView() {
        ImageView imageView = new ImageView(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                SkyConstants.Div(955), SkyConstants.Div(546), Gravity.CENTER);
        imageView.setLayoutParams(params);
        SkinManager.setBackground(imageView, R.drawable.bg_dialog);
        mLayout.addView(imageView);
    }

    private void addTitleView() {
        mTitle = new TextView(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
        params.setMargins(0, SkyConstants.Div(300), 0, 0);
        mTitle.setLayoutParams(params);
        mTitle.setTextSize(NAME_SIZE);
        mTitle.setTextColor(Color.WHITE);
        mTitle.setText(R.string.move_app);
        mLayout.addView(mTitle);
    }

    private void addDirectionView() {
        mDirectionView = new ImageView(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
        params.setMargins(0, SkyConstants.Div(390), 0, 0);

        //mDirectionView.setBackgroundResource(R.anim.plane_right_anim);
        //mDirectionView.setImageResource(R.drawable.plane_rl);
        mLayout.addView(mDirectionView, params);
        mRightAnimation = generateAnimation(160, -210, 55, 40, 60, -15, -20);
        mLeftAnimation = generateAnimation(-160, 210, 55, 40, -60, -15, 20);
        //mDirectionView.startAnimation(mRightAnimation);
    }

    private ArcTranslateAnimation generateAnimation(float fromXDelta, float toXDelta,
                                                    float fromYDelta, float toYDelta, float contorlX, float controlY, int angle) {
        ArcTranslateAnimation animation = new ArcTranslateAnimation(fromXDelta, toXDelta,
                fromYDelta, toYDelta, contorlX, controlY, angle);
        animation.setDuration(1500);
        animation.setRepeatCount(Animation.INFINITE);
        animation.setRepeatMode(Animation.RESTART);
        return animation;
    }

    public void updateNativeApp(int num, String size) {
        mNative.setText(mContext.getResources().getString(R.string.native_storage) + size);
    }

    public void updateExternalApp(int num, String size) {
        mSdcard.setText(mContext.getResources().getString(R.string.external_storage) + size);
    }

    private void addItems() {
        RelativeLayout layout = new RelativeLayout(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
        params.setMargins(0, SkyConstants.Div(360), 0, 0);

        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                SkyConstants.Div(480), SkyConstants.Div(430));
        layout.addView(generateItem(1, mContext.getResources().getString(R.string.native_storage),
                R.drawable.icon_benji, mContext.getResources().getString(R.string.move_to_sdcard)), params2);

        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
                SkyConstants.Div(480), SkyConstants.Div(430));
        params3.setMargins(SkyConstants.Div(440), 0, 0, 0);
        layout.addView(generateItem(0, mContext.getResources().getString(R.string.external_storage),
                R.drawable.icon_sdcard, mContext.getResources().getString(R.string.move_to_inter)), params3);
        mLayout.addView(layout, params);
    }

    private LinearLayout generateItem(int tag, String txt, int iconId, String btnName) {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        ImageView icon = new ImageView(mContext);
        SkinManager.setBackground(icon, iconId);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, SkyConstants.Div(40), 0, 0);
        layout.addView(icon, params);

        TextView title = new TextView(mContext);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params1.setMargins(0, SkyConstants.Div(20), 0, 0);
        title.setTextSize(SkyConstants.Dpi(28));
        //title.setLineSpacing(0f, 1.4f);
        SkinManager.setTextColor(title, R.color.appInfoColor);
        title.setGravity(Gravity.CENTER);
        title.setText(txt);
        layout.addView(title, params1);
        if (tag == 0) {
            mSdcard = title;
        } else {
            mNative = title;
        }
        Button button = new Button(mContext);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, SkyConstants.Div(136));
        params2.setMargins(0, SkyConstants.Div(25), 0, 0);
        button.setGravity(Gravity.CENTER);
        button.setTextSize(OTHER_SIZE);
        button.setTextColor(Color.WHITE);
        button.setOnFocusChangeListener(this);
        SkinManager.setBackground(button, R.drawable.block_selector);
        button.setText(btnName);
        button.setOnClickListener(this);
        button.setTag(tag);
        layout.addView(button, params2);
        return layout;
    }

//    private void startFrameAnimation(View v)
//    {
//        Drawable d = v.getBackground();
//        if(d instanceof AnimationDrawable)
//        {
//            AnimationDrawable anim = (AnimationDrawable) d;
//            if(anim != null)
//            {
//                if(anim.isRunning())
//                    anim.stop();
//                anim.setOneShot(false);
//                anim.start();
//            }
//        }
//    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if ((Integer) v.getTag() == 0) {
                mDirectionView.clearAnimation();
                mDirectionView.setBackgroundDrawable(null);
                mDirectionView.setBackgroundResource(R.drawable.plane_rl);
                mDirectionView.startAnimation(mRightAnimation);
            } else if ((Integer) v.getTag() == 1) {
                mDirectionView.clearAnimation();
                mDirectionView.setBackgroundDrawable(null);
                mDirectionView.setBackgroundResource(R.drawable.plane_lr);
                mDirectionView.startAnimation(mLeftAnimation);
            }
        }
    }
}
